/*
 * ListAssignPanel.java
 *
 * Created on 21 March 2004, 09:00
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author  Robert
 */
public class ListAssignPanel extends javax.swing.JPanel {
    
    private Collection m_allValues;
    private ValueListModel m_unassigned;
    private ValueListModel m_assigned;
    
    /** Creates new form ListAssignPanel */
    public ListAssignPanel(Collection p_allValues) {
        m_allValues = p_allValues;
        m_unassigned = new ValueListModel(new ArrayList());
        m_assigned = new ValueListModel(new ArrayList());
        initComponents();
    }
    
    public void setAssignedValues(Collection assignedValues) {
        if (assignedValues == null)
            assignedValues = new ArrayList();
        if (m_allValues != null) {
            Collection unassignedValues = new ArrayList(m_allValues);
            unassignedValues.removeAll(assignedValues);
            m_unassigned.setItems(unassignedValues);
        }
        m_assigned.setItems(assignedValues);
    }
    
    public Collection getAssignedValues() {
        return m_assigned.getCollection();
    }
    
    public void setEnabled(boolean enabled) {
        jListUnassigned.setEnabled(enabled);
        jListAssigned.setEnabled(enabled);
        jButtonAdd.setEnabled(enabled);
        jButtonRemove.setEnabled(enabled);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPaneUnassigned = new javax.swing.JScrollPane();
        jListUnassigned = new javax.swing.JList(m_unassigned);
        jScrollPaneAssigned = new javax.swing.JScrollPane();
        jListAssigned = new javax.swing.JList(m_assigned);
        jButtonAdd = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jScrollPaneUnassigned.setViewportView(jListUnassigned);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneUnassigned, gridBagConstraints);

        jScrollPaneAssigned.setViewportView(jListAssigned);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneAssigned, gridBagConstraints);

        jButtonAdd.setText("Add >>");
        jButtonAdd.setPreferredSize(new java.awt.Dimension(73, 26));
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButtonAdd, gridBagConstraints);

        jButtonRemove.setText("<< Remove");
        jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButtonRemove, gridBagConstraints);

    }//GEN-END:initComponents

    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
        // Add your handling code here:
        Object[] remove = jListAssigned.getSelectedValues();
        m_unassigned.add(remove);
        m_assigned.remove(remove);        
    }//GEN-LAST:event_jButtonRemoveActionPerformed

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        // Add your handling code here:
        Object[] add = jListUnassigned.getSelectedValues();
        m_unassigned.remove(add);
        m_assigned.add(add);                
    }//GEN-LAST:event_jButtonAddActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JList jListAssigned;
    private javax.swing.JList jListUnassigned;
    private javax.swing.JScrollPane jScrollPaneAssigned;
    private javax.swing.JScrollPane jScrollPaneUnassigned;
    // End of variables declaration//GEN-END:variables
    
}
